Graphics in R

The R language has extensive graphical capabilities.

Graphics in R may be created by many different methods including base graphics and more advanced plotting packages such as lattice.

The ggplot2 package was created by Hadley Wickham and provides a intuitive plotting system to rapidly generate publication quality graphics.

ggplot2 builds on the concept of the “Grammar of Graphics” (Wilkinson 2005, Bertin 1983) which describes a consistent syntax for the construction of a wide range of complex graphics by a concise description of their components.

Why use ggplot2

The structured syntax and high level of abstraction used by ggplot2 should allow for the user to concentrate on the visualisations instead of creating the underlying code.

On top of this central philosophy ggplot2 has:

Grammar of Graphics - How to build a plot from its components.

Grammar of Graphics - How ggplot2 builds a plot.

Grammar of Graphics - Overview of a code for ggplot2 plot.

ggplot(data = <default data set>, 
       aes(x = <default x axis variable>,
           y = <default y axis variable>,
           ... <other default aesthetic mappings>),
       ... <other plot defaults>) +

       geom_<geom type>(aes(size = <size variable for this geom>, 
                      ... <other aesthetic mappings>),
                  data = <data for this point geom>,
                  stat = <statistic string or function>,
                  position = <position string or function>,
                  color = <"fixed color specification">,
                  <other arguments, possibly passed to the _stat_ function) +

  scale_<aesthetic>_<type>(name = <"scale label">,
                     breaks = <where to put tick marks>,
                     labels = <labels for tick marks>,
                     ... <other options for the scale>) +
  
  ggtitle("Example Plot For intermediateR")+
  xlab("Plotting packages")+
  ylab("Productivity")+

  theme(plot.title = element_text(colour = "gray"),
        ... <other theme elements>)

Getting started with ggplot2

Earlier we have been working with the patient’s dataset to create a “clean and tidy” dataset.

Now we will use this dataset to demonstrate some of the plotting capabilities of ggplot2.

Now our data is clean and tidy.

library(tidyr)
library(ggplot2)
library(dplyr)
library(stringr)
library(lubridate)

patients_clean <- read.delim("patients_clean_ggplot2.txt",sep="\t")
knitr:::kable(head(patients_clean))
ID Name Race Age Sex Smokes Height Weight Birth State Pet Grade_Level Died Count Date Grade BMI Overweight
Patient-001 Demetrius White 44 Male Smoker 176.4791 77.07070 1972-02-11 New York Cat 1 TRUE -0.7440011 2015-03-04 1 24.74586 FALSE
Patient-002 Rosario White 43 Male Smoker 174.7485 83.26835 1972-07-22 Florida Cat 1 TRUE -1.0714531 2015-03-04 1 27.26799 TRUE
Patient-003 Julio Black 44 Male Non-Smoker 173.9449 85.63231 1971-11-23 Connecticut Dog 2 FALSE -0.8215636 2015-03-04 2 28.30182 TRUE
Patient-004 Lupe White 44 Male Non-Smoker 180.0289 88.72032 1971-10-03 Massachusetts Dog 1 TRUE 0.4976671 2015-03-04 1 27.37403 TRUE
Patient-005 Lavern Cat 43 Male Non-Smoker 178.6365 97.05405 1972-11-23 Kansas Cat 3 FALSE -1.2099217 2015-03-04 3 30.41397 TRUE
Patient-006 Bernie Native 43 Female Non-Smoker 159.7778 68.31247 1972-07-30 Illinois Cat 2 FALSE 0.5241618 2015-03-04 2 26.75882 TRUE

Our first ggplot2 graph

In order to produce a ggplot2 graph we need a minimum of:-

  • Data to be used in graph
  • Mappings of data to the graph (aesthetic mapping)
  • What type of graph we want to use (The geom to use).

In the code below we define the data as our cleaned patients data frame.

pcPlot <- ggplot(data=patients_clean)
class(pcPlot)
## [1] "gg"     "ggplot"
pcPlot$data[1:4,]
##            ID      Name  Race Age  Sex     Smokes   Height   Weight
## 1 Patient-001 Demetrius White  44 Male     Smoker 176.4791 77.07070
## 2 Patient-002   Rosario White  43 Male     Smoker 174.7485 83.26835
## 3 Patient-003     Julio Black  44 Male Non-Smoker 173.9449 85.63231
## 4 Patient-004      Lupe White  44 Male Non-Smoker 180.0289 88.72032
##        Birth         State Pet Grade_Level  Died      Count       Date
## 1 1972-02-11      New York Cat           1  TRUE -0.7440011 2015-03-04
## 2 1972-07-22       Florida Cat           1  TRUE -1.0714531 2015-03-04
## 3 1971-11-23   Connecticut Dog           2 FALSE -0.8215636 2015-03-04
## 4 1971-10-03 Massachusetts Dog           1  TRUE  0.4976671 2015-03-04
##   Grade      BMI Overweight
## 1     1 24.74586      FALSE
## 2     1 27.26799       TRUE
## 3     2 28.30182       TRUE
## 4     1 27.37403       TRUE

Now we can see that we have gg/ggplot object (pcPlot) and in this the data has been defined.

Important information on how to map the data to the visual properties (aesthetics) of the plot as well as what type of plot to use (geom).

pcPlot$mapping
## * NULL ->
pcPlot$theme
## list()
pcPlot$layers
## list()

The information to map the data to the plot can be added now using the aes() function.

pcPlot <- ggplot(data=patients_clean)

pcPlot <- pcPlot+aes(x=Height,Weight)

pcPlot$mapping
## $x
## Height
## 
## $y
## Weight
pcPlot$theme
## list()
pcPlot$layers
## list()

But we are still missing the final component of our plot, the type of plot to use (geom).

Below the geom_point function is used to specify a point plot, a scatter plot of x values versus y values.

pcPlot <- ggplot(data=patients_clean)

pcPlot <- pcPlot+aes(x=Height,Weight)
pcPlot <- pcPlot+geom_point()

pcPlot$mapping
## $x
## Height
## 
## $y
## Weight
pcPlot$theme
## list()
pcPlot$layers
## [[1]]
## geom_point: na.rm = FALSE
## stat_identity: na.rm = FALSE
## position_identity

Now we have all the components of our plot we need we can display the results.

pcPlot

More typically, the data and aesthetics are typically defined within ggplot function and geoms applied afterwards.

pcPlot <- ggplot(data=patients_clean,
                 mapping=aes(x=Height,Weight))
pcPlot+geom_point()

Geoms - Plot types

As we have seen, an important element of a ggplot is the geom used. Following the specification of data, the geom describes the type of plot used.

Several geoms are available in ggplot2:-

Geoms - Line plots

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Height,Weight))

pcPlot_line <- pcPlot+geom_line() 

pcPlot_line

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Height,Weight))

pcPlot_smooth <- pcPlot+geom_smooth() 

pcPlot_smooth

Geoms - Bar and frequency plots

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Height))

pcPlot_bar <- pcPlot+geom_bar() 

pcPlot_bar

# pcPlot <- ggplot(data=patients_clean,
#         mapping=aes(x=Height,))
# 
# pcPlot_bar <- pcPlot+geom_bar() 
# 
# pcPlot_bar
pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Height))

pcPlot_hist <- pcPlot+geom_histogram() 

pcPlot_hist
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Height))

pcPlot_density <- pcPlot+geom_density() 

pcPlot_density

Geoms - Box and violin plots

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Sex,y=Height))

pcPlot_boxplot <- pcPlot+geom_boxplot() 

pcPlot_boxplot

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Sex,y=Height))

pcPlot_violin <- pcPlot+geom_violin() 

pcPlot_violin

Aesthetics

In order to change the property on an aesthetic in plot into a constant value (e.g. set colour of points to red), the we can supply the colour argument to the geom_point() function.

pcPlot <- ggplot(data=patients_clean,
                 mapping=aes(x=Height,Weight))
pcPlot+geom_point(colour="red")

As we discussed earlier however, ggplot2 makes use of aesthetic mappings to assign variables in the data to the properties/aesthetics of the plot. This allows the properties of the plot to reflect variables in the data dynamically.

In these examples we supply additional information to the aes() function to define what information to display and how it is represented in the plot.

First we can recreate the plot we saw earlier.

pcPlot <- ggplot(data=patients_clean,
                 mapping=aes(x=Height,Weight))
pcPlot+geom_point()

Now we can adjust the aes mapping by supplying an argument to the colour parameter in the aes function. (Note that ggplot2 accepts “color” or “colour” as parameter name)

This simple adjustment allows for identifaction of the separation between male and female measurements for height and weight.

pcPlot <- ggplot(data=patients_clean,
                 mapping=aes(x=Height,y=Weight,colour=Sex))
pcPlot+geom_point()

Similarly the shape of points may be adjusted.

pcPlot <- ggplot(data=patients_clean,
                 mapping=aes(x=Height,y=Weight,shape=Sex))
pcPlot+geom_point()

The aesthetic mappings may be set directly in the geom_points() function as previously when specifying red. This can allow the same ggplot object to be used by different aesethetic mappings and varying geoms

pcPlot <- ggplot(data=patients_clean)
pcPlot+geom_point(aes(x=Height,y=Weight,colour=Sex))

pcPlot+geom_point(aes(x=Height,y=Weight,colour=Smokes))

pcPlot+geom_point(aes(x=Height,y=Weight,colour=Smokes,shape=Sex))

pcPlot+geom_violin(aes(x=Sex,y=Height,fill=Smokes))

- Redo the plots with parameters listed and some example plots put in.

Facets

One very useful feature of ggplot is faceting. This allows you to produce plots subsets by variables in your data.

To facet our data into multiple plots we can use the facet_wrap or facet_grid function specifying the variable we split by.

The facet_grid() is well suited to splitting the data by two factors.

Here we can plot the data with the Smokes variable as rows and Sex variable as columns.

facet_grid(Rows~Columns)

pcPlot <- ggplot(data=patients_clean,aes(x=Height,y=Weight,colour=Sex))+geom_point()
pcPlot + facet_grid(Smokes~Sex)

To split by one factor we can apply the facet_grid() function ommiting the variable before the “~”" to facet along rows in plot.

facet_grid(~Columns)

pcPlot <- ggplot(data=patients_clean,aes(x=Height,y=Weight,colour=Sex))+geom_point()
pcPlot + facet_grid(~Sex)

To split along rows in plot the variable is place before the “~.”.

facet_grid(Rows~.)

pcPlot <- ggplot(data=patients_clean,aes(x=Height,y=Weight,colour=Sex))+geom_point()
pcPlot + facet_grid(Sex~.)

The geom_wrap offers a less grid based structure but is well suited to faceting data by one variable.

By one variable we follow as similar syntax to facet_grid()

pcPlot <- ggplot(data=patients_clean,aes(x=Height,y=Weight,colour=Sex))+geom_point()
pcPlot + facet_wrap(~Smokes)

For more complex faceting both facet_grid and facet_wrap can accept combinations of variables.

Using geom_wrap

pcPlot <- ggplot(data=patients_clean,aes(x=Height,y=Weight,colour=Sex))+geom_point()
pcPlot + facet_wrap(~Pet+Smokes+Sex)

Or in a nice grid format using facet_grid() and the Smokes variable against a combination of Gender and Pet.

pcPlot + facet_grid(Smokes~Sex+Pet)

Exercise set 1

Link_to_Rmarkdown

Scales

Scales and their legends have so far been handled using ggplot2 defaults. ggplot2 offers functionality to have finer control over scales and legends using the scale methods.

Scale methods are divided into by functions by combinations of

scale_aesthetic_type

Try typing in scale_ then tab to autocomplete. This will provide some examples of the scale functions available in ggplot2.

Although different scale- functions accept some variety of paramters common arguments to scale functions include -

name - The axis or legend title

limits - Minimum and maximum of the scale

breaks - Label/tick positions along an axis

labels - Label names at each break

Controlling the X and Y scale.

Both continous and discrete X/Y scales can be controlled in ggplot2 using the

scale_(x/y)_(continous/discrete)

In this example we control the continuous sale on the x-axis by providing a name, X-axis limits, the positions of breaks (ticks/labels) and the labels to place at breaks.

pcPlot +
  geom_point() +
  facet_grid(Smokes~Sex)+
  scale_x_continuous(name="height ('cm')",
                     limits = c(100,200),
                     breaks=c(125,150,175),
                     labels=c("small","justright","tall"))

Similary control over discrete scales is shown below.

pcPlot <- ggplot(data=patients_clean,aes(x=Sex,y=Height))
pcPlot +
  geom_violin(aes(x=Sex,y=Height)) +
  scale_x_discrete(labels=c("Girls","Guys"))

Multiple X/Y scales can be combined to give full control of axis marks.

pcPlot <- ggplot(data=patients_clean,aes(x=Sex,y=Height,fill=Smokes))
pcPlot +
  geom_violin(aes(x=Sex,y=Height)) +
  scale_x_discrete(labels=c("Guys","Girls"))+
  scale_y_continuous(breaks=c(160,180),labels=c("Petite","Tall"))

Controlling colour, shape, size and alpha scales.

When using fill,colour,shape,size or alpha aesthetic mappings the scales are automatically selected for you and the appropriate legends created.

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=Sex))
pcPlot + geom_point(size=4)

In the above example the discrete colours for the Sex variable was selected by default.

Manual control of discrete variables can be performed using scale_aes_Of_Interest_manual with the values parameter. Additionally an updated name for the legend is provided.

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=Sex))
pcPlot + geom_point(size=4) + 
  scale_color_manual(values = c("Green","Purple"),
                     name="Gender")

Here we have specified the colours to be used (hence the manual) but when the number of levels to a variable are high this may be impractical and often we would like ggplot2 to choose colours from a scale of our choice.

The brewer set of scale functions allow the user to make use of a range of pallets available from colorbrewer.

  • Diverging

BrBG, PiYG, PRGn, PuOr, RdBu, RdGy, RdYlBu, RdYlGn, Spectral

  • Qualitative

Accent, Dark2, Paired, Pastel1, Pastel2, Set1, Set2, Set3

  • Sequential

Blues, BuGn, BuPu, GnBu, Greens, Greys, Oranges, OrRd, PuBu, PuBuGn, PuRd, Purples, RdPu, Reds, YlGn, YlGnBu, YlOrBr, YlOrRd

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=Pet))
pcPlot + geom_point(size=4) + 
  scale_color_brewer(palette = "Set2")

For more details on palette sizes and styles visit the colorbrewer website.

So far we have looked a qualitative scales but ggplot2 offers much functionality for continuous scales such as for size, alpha (transparancy), colour and fill.

scale_alpha_continuous() - For Transparancy scale_size_continuous() - For control of size.

Both these functions accept the range of alpha/size to be used in plotting.

Below the range of alpha to be used in plot is limited to between 0.5 and 1

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,alpha=BMI))
pcPlot + geom_point(size=4) + 
  scale_alpha_continuous(range = c(0.5,1))

Below the range of sizes to be used in plot is limited to between 3 and 6

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,size=BMI))
pcPlot + geom_point(size=4) + 
  scale_size_continuous(range = c(3,6))

The limits of the scale can also be controlled.

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,size=BMI))
pcPlot + geom_point() + scale_size_continuous(range = c(3,6), limits = c(25,40))
## Warning: Removed 14 rows containing missing values (geom_point).

What points of scale to be displayed and the labels for scale can alos be controlled.

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,size=BMI))
pcPlot + geom_point() + scale_size_continuous(range = c(3,6), limits = c(25,40),breaks=c(25,35),labels=c("Good","Not So Good"))
## Warning: Removed 14 rows containing missing values (geom_point).

Control of colour/fill scales can be best achieved through the gradient subfunctions of scale.

scale_(colour/fill)gradient - 2 colour gradient (eg. low to high BMI) scale(colour/fill)_gradient2 - Diverging colour scale with a midpoint colour (e.g. Down, No Change, Up)

Both functions take a common set of arguments:-

  • low - Colour for low end of gradient scale
  • high - Colour for high end of gradient scale.
  • na.value - Colour for any NA values.

An example using scale_colour_gradient below set the low and high end colours to White and Red respectively

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=BMI))
pcPlot + geom_point(size=4,alpha=0.8) + 
  scale_colour_gradient(low = "White",high="Red")

Similarly we can use the scael_colour_gradient2 function which allows for the specification of a midpoint value and associated colour.

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=BMI))
pcPlot + geom_point(size=4,alpha=0.8) + 
  scale_colour_gradient2(low = "Blue",mid="Black",high="Red",midpoint = median(patients_clean$BMI))

As with previous continous scales, limits and custom labels in scale legend can be added.

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=BMI))
pcPlot + geom_point(size=4,alpha=0.8) + scale_colour_gradient2(low = "Blue",
                         mid="Black",
                         high="Red",
                         midpoint = median(patients_clean$BMI),
                         breaks=c(25,30),labels=c("Low","High"),
                         name="Body Mass Index")

Multiple scales may be combined to create high customisable plots and scales

pcPlot <- ggplot(data=patients_clean,
                 aes(x=Height,y=Weight,colour=BMI,shape=Sex))
pcPlot + geom_point(size=4,alpha=0.8)+ scale_shape_discrete(name="Gender") +scale_colour_gradient2(low = "Blue",mid="Black",high="Red",midpoint = median(patients_clean$BMI),
                         breaks=c(25,30),labels=c("Low","High"),
                         name="Body Mass Index")

Statistical transformations.

In ggplot2 many of the statistical transformations are performed without any direct specification e.g. geom_histogram() will use stat_bin() function to generate bin counts to be used in plot.

An example of statistical methods which are very useful ggplot2 include the stat_smooth() and stat_summary() functions.

The stat_smooth() function can be used to fit a line to the data being displayed.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))
pcPlot+geom_point()+stat_smooth()

By default a “loess” smooth line is plotted by stat_smooth. Other methods available include lm, glm,gam,rlm.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))
pcPlot+geom_point()+stat_smooth(method="lm")

A useful feature of ggplot2 is that it uses previously defined grouping when performing smoothing.

If i add colour by Sex as an aesthetic mapping then two smooth lines are drawn, one for each sex.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height,colour=Sex))
pcPlot+geom_point()+stat_smooth(method="lm")

This behaviour can be overridden by specifying an aes within the stat_smooth() function and setting inherit.aes to FALSE.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height,colour=Sex))
pcPlot+geom_point()+stat_smooth(aes(x=Weight,y=Height),method="lm",inherit.aes = F)

Another useful method is stat_summary() which allows for the custom statistical function to be performed and the visualised.

The fun.y parameter allows the specification of a function to apply to the y variable for every value of x.

In this example we use it to plot the quantiles of the Female and Male Height data

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Sex,y=Height))+geom_jitter()
pcPlot+stat_summary(fun.y=quantile,geom="point",colour="purple",size=8)

Transforming data after statistics.

Themes

Themes specify the details of data independent elements of the plot. This includes titles, background colour, text fonts etc.

The graphs created so far have all used the default themes ,theme_grey(), but ggplot2 allows for the specification of theme used.

Predefined themes can be applied to a ggplot2 object using a family of functions theme_style()

In the example below the minimal theme is applied to the scatter plot seen earlier.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))+geom_point()
pcPlot+theme_minimal()

Several predifined themes are available within ggplot2 including:

  • theme_bw

  • theme_classic

  • theme_dark

  • theme_gray

  • theme_light

  • theme_linedraw

  • theme_minimal

Packages such as ggthemes also contain many useful collections of predined theme_style functions.

As well as making use of predifened theme styles, ggplot2 allows for control over the attributes and elements within a plot through a collection of related functions and attributes.

theme() is the global function used to set the collections of elements’ attributes for the current plot.

Within the theme functions there are 3 general graphic elements which may be controlled:-

  • rect
  • line
  • text

and 5 groups of related elements:-

  • axis
  • legend
  • strip
  • graphics
  • Global plot

When required these elements may be specified by the use of element functions including:

  • element_line()
  • element_text()
  • element_rect()

and additionally element_blank() to set an element to “blank”

To better demonstrate this, the below example sets the theme to have all rect, line and text elements as “blank”. We can see we get no axis, legend or labels.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))+geom_point()
pcPlot+theme(
            rect = element_blank(),
            line = element_blank(),
            text = element_blank()
           )

When defining a custom theme, it is useful to create a function to allow for the alteration of specific elements while maintaining the overall style.

In the example below , a function is defined ,theme_black, to create a black theme while allowing for the specification of text size and font. Here the full list of potential element attributes is listed.

theme_black <- function(base_size = 12, base_family = "Helvetica") {
    theme(
    line =               element_line(colour = "black", size = 0.5, linetype = 1,
                            lineend = "butt"),
    rect =               element_rect(fill = "white", colour = "black", size = 0.5, linetype = 1),
    text =               element_text(family = base_family, face = "plain",
                            colour = "black", size = base_size,
                            hjust = 0.5, vjust = 0.5, angle = 0, lineheight = 0.9,
                            margin=margin(4),debug=F),
    axis.text =          element_text(size = rel(0.8), colour = "white",margin=margin(5,5,10,5,"pt")),
    strip.text =         element_text(size = rel(0.8), colour = "white"),

    axis.line =          element_blank(),
    axis.text.x =        element_text(vjust = 1),
    axis.text.y =        element_text(hjust = 1),
    axis.ticks =         element_line(colour = "white", size = 0.2),
    axis.title =         element_text(colour = "white"),
    axis.title.x =       element_text(vjust = 1),
    axis.title.y =       element_text(angle = 90),
    axis.ticks.length =  unit(0.3, "lines"),

    legend.background =  element_rect(colour = NA),
    legend.margin =      unit(0.2, "cm"),
    legend.key =         element_rect(fill = "black", colour = "white"),
    legend.key.size =    unit(1.2, "lines"),
    legend.key.height =  NULL,
    legend.key.width =   NULL,
    legend.text =        element_text(size = rel(0.8), colour = "white"),
    legend.text.align =  NULL,
    legend.title =       element_text(size = rel(0.8), face = "bold", hjust = 0, colour = "white"),
    legend.title.align = NULL,
    legend.position =    "right",
    legend.direction =   "vertical",
    legend.justification = "center",
    legend.box =         NULL,

    panel.background =   element_rect(fill = "black", colour = NA),
    panel.border =       element_rect(fill = NA, colour = "white"),
    panel.grid.major =   element_line(colour = "grey20", size = 0.2),
    panel.grid.minor =   element_line(colour = "grey5", size = 0.5),
    panel.margin =       unit(0.25, "lines"),

    strip.background =   element_rect(fill = "grey30", colour = "grey10"),
    strip.text.x =       element_text(),
    strip.text.y =       element_text(angle = -90),

    plot.background =    element_rect(colour = "black", fill = "black"),
    plot.title =         element_text(size = rel(1.2)),
    plot.margin =        unit(c(1, 1, 0.5, 0.5), "lines"),

    complete = TRUE
  )
}

Now the function has been defined it can be applied to the plot.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))+geom_point(colour="White")
pcPlot+theme_black(base_size = 15)

Adding titles for plot and labels.

So far no plot titles have been specified. Plot titles can be specified using the labs functions.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))+geom_point()
pcPlot+labs(title="Weight vs Height",y="Height (cm)")

or specified using the ggtitle and xlab/ylab functions.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))+geom_point()
pcPlot+ggtitle("Weight vs Height")+ylab("Height (cm)")

http://docs.ggplot2.org/current/theme.html

Saving plots

Plots produced by ggplot can be saved from the interactive viewer as with standard plots.

The ggsave() function allows for additional arguments to be specified including the type, resolution and size of plot.

By default ggsave() will used the size of your current graphics window when saving plots so it may be important to specify width and height arguments desired.

pcPlot <- ggplot(data=patients_clean,
        mapping=aes(x=Weight,y=Height))+geom_point()
ggsave(pcPlot,filename = "anExampleplot.png",width = 15,height = 15,units = "cm")

Exercises 2

Any more questions?

References.

Scale added automatically colour/color alpha size

Show turning discrete from continous with factor type shape

Discrete Continuous Manual

Stats

stat_smooth stat_summary

Themes

Blank_theme theme_get and theme_set ggthemes Themes to adjust labels. Creating your own theme XKCD theme